package com.cyt.website.service.impl;

import cn.hutool.core.util.StrUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.ErrorCode;
import com.cyt.sys.model.po.SysFile;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.model.vo.SysFileVo;
import com.cyt.sys.service.SysFileService;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.website.dao.WebsiteBannerDao;
import com.cyt.website.model.po.WebsiteBanner;
import com.cyt.website.model.vo.WebsiteBannerVo;
import com.cyt.website.service.WebsiteBannerService;
import com.cyt.website.util.cache.CacheService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 官网banner图管理-官网banner图管理Service层实现类
 *
 * @author 聪聪
 * @date 2019-8-12 14:23:55
 */
@Service("websiteBannerService")
public class WebsiteBannerServiceImpl extends BaseServiceImpl<WebsiteBanner> implements WebsiteBannerService {

    @Resource
    private SysFileService sysFileService;
    @Resource
    private CacheService cacheService;
    @Resource
    private WebsiteBannerDao websiteBannerDao;

    @Autowired
    public void setBaseDao(WebsiteBannerDao websiteBannerDao) {
        this.baseDao = websiteBannerDao;
    }

    /**
     * 查询对象集合
     *
     * @param websiteBannerVo 对象
     * @return List 对象集合
     * @author 聪聪
     * @date 2019-8-12 14:23:55
     */
    @Override
    public List<WebsiteBanner> listExample(WebsiteBannerVo websiteBannerVo) {
        Example example = new Example(WebsiteBanner.class);
        example.setOrderByClause(" order_num desc");
        Example.Criteria criteria = example.createCriteria();
        if (StrUtil.isNotEmpty(websiteBannerVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", websiteBannerVo.getRemoveTag());
        }
        if (StrUtil.isNotEmpty(websiteBannerVo.getStatus())) {
            criteria.andEqualTo("status", websiteBannerVo.getStatus());
        }
        if (StrUtil.isNotEmpty(websiteBannerVo.getTitle())) {
            criteria.andLike("title", "%" + websiteBannerVo.getTitle() + "%");
        }
        return websiteBannerDao.selectByExample(example);
    }

    /**
     * 分页查询
     *
     * @param websiteBannerVo 接受参数对象
     * @param pageNum          分页数
     * @param pageSize         每页显示条数
     * @return ReturnResult
     * @author 聪聪
     * @date 2019-8-12 14:23:55
     */
    @Override
    public ReturnResult page(WebsiteBannerVo websiteBannerVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<WebsiteBanner> list = listExample(websiteBannerVo);
        PageInfo<WebsiteBanner> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 添加或更新
     *
     * @param websiteBannerVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author 聪聪
     * @date 2019-8-12 14:23:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(WebsiteBannerVo websiteBannerVo, SysUser sysUser) {
        if (StrUtil.isEmpty(websiteBannerVo.getId())) {
            WebsiteBanner websiteBanner = new WebsiteBanner(sysUser);
            websiteBanner.setTitle(websiteBannerVo.getTitle());
            websiteBanner.setPhoto(websiteBannerVo.getPhoto());
            websiteBanner.setPath(getFilePath(websiteBannerVo.getPhoto()));
            websiteBanner.setUrl(websiteBannerVo.getUrl());
            websiteBanner.setType(websiteBannerVo.getType());
            websiteBanner.setContent(websiteBannerVo.getContent());
            websiteBanner.setStatus(websiteBannerVo.getStatus());
            websiteBanner.setOrderNum(websiteBannerVo.getOrderNum());
            websiteBanner.setSeoKeywords(websiteBannerVo.getSeoKeywords());
            websiteBanner.setSeoDescription(websiteBannerVo.getSeoDescription());
            int num = websiteBannerDao.insert(websiteBanner);
            if (num > 0) {
                cacheService.evictWebsiteBanner();
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_INSERT);
            }
        } else {
            WebsiteBanner websiteBanner = websiteBannerDao.selectByPrimaryKey(websiteBannerVo.getId());
            websiteBanner.setTitle(websiteBannerVo.getTitle());
            websiteBanner.setPhoto(websiteBannerVo.getPhoto());
            websiteBanner.setPath(getFilePath(websiteBannerVo.getPhoto()));
            websiteBanner.setUrl(websiteBannerVo.getUrl());
            websiteBanner.setType(websiteBannerVo.getType());
            websiteBanner.setContent(websiteBannerVo.getContent());
            websiteBanner.setStatus(websiteBannerVo.getStatus());
            websiteBanner.setOrderNum(websiteBannerVo.getOrderNum());
            websiteBanner.setSeoKeywords(websiteBannerVo.getSeoKeywords());
            websiteBanner.setSeoDescription(websiteBannerVo.getSeoDescription());
            websiteBanner.setUpdateTime(new Date());
            websiteBanner.setUpdateUser(sysUser.getId());
            int num = websiteBannerDao.updateByPrimaryKey(websiteBanner);
            if (num > 0) {
                cacheService.evictWebsiteBanner();
                return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
            } else {
                return new ReturnResult(ErrorCode.CODE_1, BaseConstant.DATABASE_ERROR_UPDATE);
            }
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param websiteBannerVo 参数接受对象
     * @param sysUser 当前用户
     * @return ReturnResult
     * @author 聪聪
     * @date 2019-8-12 14:23:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(WebsiteBannerVo websiteBannerVo, SysUser sysUser) {
        //要修改的字段
        WebsiteBanner websiteBanner = new WebsiteBanner();
        websiteBanner.setRemoveTag(BaseConstant.REMOVE_TAG_YES);
        websiteBanner.setUpdateUser(sysUser.getId());
        websiteBanner.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteBannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteBannerVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteBannerDao.updateByExampleSelective(websiteBanner, example);
        cacheService.evictWebsiteBanner();
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param websiteBannerVo 参数接受对象
     * @param sysUser      当前用户
     * @return ReturnResult
     * @author 聪聪
     * @date 2019-8-12 14:23:55
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(WebsiteBannerVo websiteBannerVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(WebsiteBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteBannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteBannerVo.getId().split(",")));
        }
        websiteBannerDao.deleteByExample(example);
        cacheService.evictWebsiteBanner();
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    @Override
    public ReturnResult updateStatus(WebsiteBannerVo websiteBannerVo, SysUser sysUser) {
        //要修改的字段
        WebsiteBanner banner = new WebsiteBanner();
        banner.setStatus(websiteBannerVo.getStatus());
        banner.setUpdateUser(sysUser.getId());
        banner.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(WebsiteBanner.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(websiteBannerVo.getId())) {
            criteria.andIn("id", Arrays.asList(websiteBannerVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", BaseConstant.REMOVE_TAG_NO);
        websiteBannerDao.updateByExampleSelective(banner, example);
        cacheService.evictWebsiteBanner();
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    private String getFilePath(String ObjectId) {
        SysFileVo sysFileVo = new SysFileVo(ObjectId);
        List<SysFile> sysFileList = sysFileService.listExample(sysFileVo);
        if (sysFileList != null && sysFileList.size() == 1) {
            return sysFileList.get(0).getPath();
        }
        return "";
    }
}